<?php # 5.2.4

class CMSoldier
{
	private
		$id			= '',
		$life		= CM_LIFE,			# vida del soldado
		$power_min	= 2,				# fuerza, golpe minimo
		$power_max	= 5,				# fuerza, golpe maximo
		$stamina	= 100,				# stamina, aplicable al ataque
		$resistance	= 200				# resistencia, aplicable para la defensa
	;
	
	final public function __construct($id, $max=1, $min=3, $stamina=100, $resistance=100)
	{
		$this->id = $id;
		$this->power_min = $min;
		$this->power_max = $max;
		$this->stamina	 = $stamina;
		$this->resistance= $resistance;
	}
	
	final public
	function __get($name)
	{
		return $this->$name;
	}
	
	final public
	function __set($name, $value)
	{
		return $this->$name = $value;
	}
	
	final public function __call($fnc, $args){}
	
	final public
	function __toString()
	{
		$str = '';
		foreach($this as $k=>$v)
			$str .="$k\t\t".json_encode($v)."\n";
		return $str;
	}
	
	final public 
	function attack(CMSoldier $defend)
	{	
		if($this->stamina < 1)
		{
			echo("El soldado esta cansado, descansa\n");
			$this->rest();
			return;
		}
		
		$hit = rand($this->power_min*$this->stamina, $this->power_max*$this->stamina);
		
		if($hit == $this->power_max*$this->stamina)
		{
			$this->upgrade();
		}
		
		echo("\t{$this->id} golpea por $hit\n");
		if(($damage = $defend->hit( $hit )) > 0)
		{	
			$this->life -= $damage;
			$this->stamina -= ($st = round($hit / $damage));
			echo("\tPierdo $st de stamina y $damage de vida\n");
		}
		echo("\n");
	}
	
	final public 
	function hit($hit) 
	{	
		$this->resistance -= ($res = round($hit / $this->resistance));
		
		if(($damage = $hit-$this->resistance) < 1)
		{
			$this->life -= $res;
			echo("\tCubre golpe ($res) y devuelve golpe por $damage \n");
			return abs($damage);
		}
		echo("\t$this->id recibe golpe por $damage\n");
		$this->life -= $damage;
		return 0;
	}
	
	final public
	function upgrade()
	{
		echo("\t$this->id es ascendido\n");
		$this->rest();
		$this->power_min++;
		$this->power_max++;
	}
	
	final public
	function rest()
	{
		$this->stamina = min(
							round( max($this->stamina, 1) * CM_STAMINA_RCV ),
							CM_STAMINA_MAX
						);
	}
	
	final public
	function isLive()
	{
		return $this->life > 0;
	}
}
?>